package com.android.aishangauction.fragment.member;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.aishangauction.R;
import com.android.aishangauction.fragment.BaseLazyFragment;
import com.android.aishangauction.model.data;
import com.android.aishangauction.model.dataCallback;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.MyLog;
import com.android.aishangauction.utils.ObjectUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/9/7.
 */

public class Fragment_FeedBack extends BaseLazyFragment {


    @BindView(R.id.add_detailaddress)
    TextView add_detailaddress;
//    TextView aboutusVersion;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;


    private boolean networkConnected;


    public Fragment_FeedBack() {


    }


    public static Fragment_FeedBack newInstance() {
        Fragment_FeedBack fragment = new Fragment_FeedBack();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {


    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_feedback, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }


    private void initUI() {
        String versionCode = ObjectUtils.getVersionName(mcontext);
        //  aboutusVersion.setText("当前版本" + versionCode);

    }


    @OnClick({R.id.feedback_cancel,R.id.feedback_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.feedback_cancel:
                getActivity().finish();
                break;
            case R.id.feedback_confirm:
                String content = add_detailaddress.getText().toString().trim();
                if ("".equals(content)) {
                    ObjectUtils.toast(getActivity(), "内容不能为空");
                    return;
                }
            Publish(content);
                break;
        }
    }
    private void Publish(String content){
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address =  ConstantsUrl.domain+ConstantsUrl.MEMBERFEEDBACK;
        MyLog.e("地址", address);
        if (networkConnected) {
            HashMap<String, String> map = new HashMap<>();
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            map.put("content",content);
           HttpUtils.post(address, header, map, new dataCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            ObjectUtils.toast(mcontext,e.getMessage());
                        }

                        @Override
                        public void onResponse(data response, int id) {

                            if (response.getCode() == 100002) {
                                ObjectUtils.toast(mcontext, response.getMsg());
                                getActivity().finish();
                            } else {
                                ObjectUtils.toast(mcontext, response.getMsg());
                            }

                        }
                    }

            );
        } else {
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }

}

